﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace GoogleTranslateSidebarPlugin
{
    public partial class Translate : UserControl
    {
        private Notepad_X.Main main = Notepad_X.Static.MainInstance;

        public System.Collections.Hashtable LanguageList = new System.Collections.Hashtable();
        
        public Translate()
        {
            LanguageList.Add("English", "en");
            LanguageList.Add("Greek", "el");
            LanguageList.Add("Italian", "it");
            LanguageList.Add("Russian", "ru");
            LanguageList.Add("German", "de");
            LanguageList.Add("Albanian", "sq");
            LanguageList.Add("Afrikaans", "af");
            LanguageList.Add("Vietnamese", "vi");
            LanguageList.Add("Bulgarian", "bg");
            LanguageList.Add("Belarusian", "be");
            LanguageList.Add("Chinese Simplified", "zh-CN");
            LanguageList.Add("Chinese  Traditional", "zh-TW");
            LanguageList.Add("Dutch", "nl");
            LanguageList.Add("Danish", "da");
            LanguageList.Add("Estonian", "et");
            LanguageList.Add("French", "fr");
            LanguageList.Add("Finnish", "fi");
            LanguageList.Add("Hebrew", "iw");
            LanguageList.Add("Japanese", "ja");
            LanguageList.Add("Indonesian", "id");
            LanguageList.Add("Irish", "ga");
            LanguageList.Add("Icelandic", "is");
            LanguageList.Add("Korean", "ko");
            LanguageList.Add("Croatian", "hr");
            LanguageList.Add("Latvian", "lv");
            LanguageList.Add("Lithuanian", "lt");
            LanguageList.Add("Maltese", "mt");
            LanguageList.Add("Norwegian", "no");
            LanguageList.Add("Hungarian", "hu");
            LanguageList.Add("Persian", "fa");
            LanguageList.Add("Portuguese", "pt");
            LanguageList.Add("Polish", "pl");
            LanguageList.Add("Romanian", "ro");
            LanguageList.Add("Spanish", "es");
            LanguageList.Add("Serbian", "sr");
            LanguageList.Add("Slovak", "sk");
            LanguageList.Add("Slovenian", "sl");
            LanguageList.Add("Swedish", "sv");
            LanguageList.Add("Czech", "cs");
            LanguageList.Add("Thai", "th");
            LanguageList.Add("Turkish", "tr");
            LanguageList.Add("Yiddish", "yi");

            InitializeComponent();

            String[] keys= new string[LanguageList.Keys.Count];
            LanguageList.Keys.CopyTo(keys, 0);
            Array.Sort(keys);

            from_Combo.Items.AddRange(keys);
            to_Combo.Items.AddRange(keys);

            from_Combo.SelectedIndex = 10;
            to_Combo.SelectedIndex = 10;
        }


        private void button1_Click(object sender, EventArgs e)
        {
            String selectedText = null;
            String pair = LanguageList[from_Combo.SelectedItem].ToString() + "|" + LanguageList[to_Combo.SelectedItem].ToString();

            int id = main.get_selectedID();
            if (id != -1)
            {
                selectedText = main.Documents[id].selectedText();
            }
            if (!String.IsNullOrEmpty(selectedText))
            {
                translatedText.Text = Translator.TranslateText(selectedText, pair);
            }
            plugin.SaveSettings(this);
        }

        private void Translate_Load(object sender, EventArgs e) {
            for (int i = 0; i < Notepad_X.Main.app_settings.pluginsSettings.Count; i++) {
                if (Notepad_X.Main.app_settings.pluginsSettings[i].name.ToLower() == "googletranslatesidebarplugin") {
                    for (int j = 0; j < Notepad_X.Main.app_settings.pluginsSettings[i].values.Count; j++) {
                        if (Notepad_X.Main.app_settings.pluginsSettings[i].values[j].Key == "from") {
                            from_Combo.SelectedItem = Notepad_X.Main.app_settings.pluginsSettings[i].values[j].Value;
                        }
                        if (Notepad_X.Main.app_settings.pluginsSettings[i].values[j].Key == "to") {
                            to_Combo.SelectedItem = Notepad_X.Main.app_settings.pluginsSettings[i].values[j].Value;
                        }
                    }
                }
            }
        }

    }
}
